package com.centling.smartSealForPhone.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.centling.smartSealForPhone.BuildConfig;
import com.centling.smartSealForPhone.activity.LoginActivity;
import com.centling.smartSealForPhone.utils.LogUtil;
import com.centling.smartSealForPhone.utils.ActivityCountManager;

import static com.centling.smartSealForPhone.receiver.JPushMessageReceiver.MSG_INFO;
import static com.centling.smartSealForPhone.receiver.JPushMessageReceiver.START_FROM_MSG;

/**
 * JPushAssistReceiver
 *
 * @author fionera
 * @date 17-7-5
 */

public class JPushAssistReceiver
        extends BroadcastReceiver {
    public static final String ACTION_ASSIST_OPERATE = BuildConfig.APPLICATION_ID + "" +
            ".ACTION_ASSIST_OPERATE";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        LogUtil.d("JPushAssistReceiver Receive Msg:" + intent
                .getAction() + ", extras: " + (bundle != null ? bundle.toString() : "null"));

        if (TextUtils.equals(JPushAssistReceiver.ACTION_ASSIST_OPERATE, intent.getAction())) {
            Activity currentActivity = ActivityCountManager.currentActivity();
            if (currentActivity == null) {
                context.startActivity(getIntentForJump(context, intent.getStringExtra(MSG_INFO),
                        LoginActivity.class).putExtra(START_FROM_MSG, true));
            } else {
                context.startActivity(getIntentForJump(context, intent.getStringExtra(MSG_INFO),
                        currentActivity.getClass()));
            }
        }
    }

    private Intent getIntentForJump(Context context, String message, Class currentActivity) {
        return new Intent(context, currentActivity).addFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra(
                "newIntentType", "newIntentType").putExtra(MSG_INFO,
                message);
    }
}
